package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.domain.PageResult;
import com.itheima.domain.pojo.*;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.service.EmpLogService;
import com.itheima.service.EmpService;
import com.itheima.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 员工管理
 */
@Slf4j
@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;
    @Autowired
    private EmpLogService empLogService;

    //    @Override
//    public PageResult<Emp> page(Integer page, Integer pageSize, String name, Integer gender, LocalDate begin, LocalDate end){
//        //1.开启分页，设置分页参数
//        PageHelper.startPage(page, pageSize);
//        //调用mapper查询
//        List<Emp> empList = empMapper.list(name, gender, begin, end);
//        //2.强转为Page
//        Page<Emp> p = (Page<Emp>) empList;
//        //3.封装PageResult并返回给controller
//        return new PageResult<Emp>(p.getTotal(), p.getResult());
//    }
    @Override
    public PageResult page(EmpQueryParam empQueryParam){
        //1.开启分页，设置分页参数
        PageHelper.startPage(empQueryParam.getPage(), empQueryParam.getPageSize() );
        //调用mapper查询
        List<Emp> empList = empMapper.list(empQueryParam);
        //2.强转为Page
        Page<Emp> p = (Page<Emp>) empList;
        //3.封装PageResult并返回给controller
        return new PageResult(p.getTotal(), p.getResult());
    }
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void save (Emp emp){
        try {


            //设置默认值
            emp.setCreateTime(LocalDateTime.now());
            emp.setUpdateTime(LocalDateTime.now());
            empMapper.insert(emp);
            log.info("保存员工信息成功！员工信息：{}", emp);

//            int i = 1 / 0;

            Integer empId = emp.getId();
            List<EmpExpr> exprList = emp.getExprs();
            if (exprList != null && exprList.isEmpty()) {
                exprList.forEach(expr -> expr.setEmpId(emp.getId()));
                empExprMapper.insertBatch(exprList);
            }
        }finally {
            EmpLog empLog = new EmpLog(emp.getId(), LocalDateTime.now(), emp.toString());
            empLogService.insertLog(empLog);
        }
    }
    @Override
    public List<Emp> niager(){
        return empMapper.niager();
    }

    @Transactional
    @Override
    public void deleteByIds(List<Integer> ids) {
        //1. 根据ID批量删除员工基本信息
        empMapper.deleteByIds(ids);

        //2. 根据员工的ID批量删除员工的工作经历信息
        empExprMapper.deleteByEmpIds(ids);
    }

    @Override
    public void update(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateById(emp);

        empExprMapper.deleteByEmpIds(Arrays.asList(emp.getId()));

        Integer empId = emp.getId();
        List<EmpExpr> exprList = emp.getExprs();
        if (exprList != null && exprList.isEmpty()) {
            exprList.forEach(expr -> expr.setEmpId(emp.getId()));
            empExprMapper.insertBatch(exprList);
        }
    }

    @Override
    public Emp getInfo(Integer id) {
        return empMapper.getById(id);
    }

    @Override
    public LoginInfo login(Emp emp) {
        Emp empLogin = empMapper.getUsernameAndPassword(emp);
        if(empLogin != null){
            //1. 生成JWT令牌
            Map<String,Object> dataMap = new HashMap<>();
            dataMap.put("id", empLogin.getId());
            dataMap.put("username", empLogin.getUsername());

            String jwt = JwtUtils.generateJwt(dataMap);
            LoginInfo loginInfo = new LoginInfo(empLogin.getId(), empLogin.getUsername(), empLogin.getName(), jwt);
            return loginInfo;
        }
        return null;
    }
}